package com.ds.common.org.expression.org;

import com.ds.common.expression.ParseException;
import com.ds.common.expression.function.Function;
import com.ds.org.Org;
import com.ds.org.Person;
import com.ds.org.PersonNotFoundException;
import com.ds.server.OrgManagerFactory;

import java.util.Stack;

 /** 
  * 取得指定人员的组织
 * <p>Title: JDSORG组织机构中间件</p>
 * <p>Description: </p>
 * <p>Copyright: jds Copyright (c) 2003-2008</p>
 * <p>Company: www.justdos.net</p>
 * @author wenzhangli
 * @version 2.0
 */
public class GetOrgByPerson extends Function {
	public GetOrgByPerson() {
		numberOfParameters = 1;
	}

	public void run(Stack stack) throws ParseException
	{
		checkStack(stack); // check the stack

		stack.push(perform(stack.pop()));
	}

	protected Org[] perform(Object param1) throws ParseException {
		if (param1 == null) {
			return new Org[0];
		}
		//人员的标识
		if (param1 instanceof String) {
			String personId = (String)param1;
			try {
				Person person = OrgManagerFactory.getOrgManager().getPersonByID(personId);
				if (person != null) {
					return (Org[]) person.getOrgList().toArray(new Org[person.getOrgList().size()]);
				}
				return new Org[0];
			} catch (PersonNotFoundException e) {
				return new Org[0];
			}
		}
		//人员对象
		if (param1 instanceof Person) {
			Person person = (Person)param1;
			if (person != null) {
				return (Org[]) person.getOrgList().toArray(new Org[person.getOrgList().size()]);
			}
			return new Org[0];
		}
		throw new ParseException("Invalid parameter type in:" + this.getClass().getName() + ", param1 is " + param1.getClass().getName());
	}
}